<template>
  <div class="uploadPostContainer">
    <div class="typeContainer">
      <span>类型</span>
      <el-dropdown @command="handleCommand">
        <span class="el-dropdown-link">
          {{postType == "" ? '请选择贴文类型':postType}}<i class="el-icon-arrow-down el-icon--right"></i>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item :command="item.type" v-for="(item,index) in ArticleType" :key="index">{{item.type}}</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
    <div class="contentInput">
      <span>内容</span>
      <el-input
        type="textarea"
        :rows="2"
        placeholder="请输入内容"
        v-model="postContent"
      >
      </el-input>
    </div>
    <div>
      <div @click="handlePost(true)" v-show="postId != undefined" class="btn">
        修 改
      </div>
      <div @click="handlePost(false)" v-show="postId == undefined" class="btn">
        发 布
      </div>
    </div>
  </div>
</template>

<script>
import { publishPost } from "@/utils/api";
import { editPostById } from "@/utils/api";
import { getPostByPostId } from "@/utils/api";
export default {
  props: ["postId", "userInfo"],
  data() {
    return {
      postType: "",
      postContent: "",
      post: {},
      ArticleType: [
        //贴文类型
        { type: "推荐", value: "推荐" },
        { type: "学习", value: "学习" },
        { type: "游戏", value: "游戏" },
        { type: "美食", value: "美食" },
        { type: "历史", value: "历史" },
      ],
    };
  },
  methods: {
    async handlePost(flag) {
      let type = this.postType;
      let content = this.postContent;
      if (type != "" && content != "" && this.userInfo != undefined) {
        if (flag) {
          // 修改贴文，封装参数，贴文id为postId,贴文类型type，贴文内容content
          let params = {
            id: this.postId,
            upId: this.userInfo.userId,
            type,
            content,
          };
          // 发送修改请求
          let result = await editPostById(params);
          console.log(result);
          if (result.code == 200) {
            this.$message({
              message: "修改贴文成功!",
              type: "success",
            });
            this.$router.push({ path: "/bingo/User_home/upload/my-post" });
          }
        } else {
          // 封装发布贴文的参数
          let params = {
            type,
            content,
            upId: this.userInfo.userId,
          };
          // 发送发布贴文的请求
          let result = await publishPost(params);
          if (result.code == 200) {
            this.$message({
              message: "发布贴文成功!",
              type: "success",
            });
            this.$router.push({ path: "/bingo/User_home/upload/my-post" });
          }
        }
      } else {
        // 输入格式不对
        this.$message({
          message: "输入格式不对!",
          type: "warning",
        });
      }
    },
    async initPostByPostId(postId) {
      let result = await getPostByPostId(postId);
      this.post = result.data.resultMap.post;
      this.postType = result.data.resultMap.post.type;
      this.postContent = result.data.resultMap.post.content;
    },
    // 用户选择贴文类型
    handleCommand(command) {
        this.postType = command;
      }
  },
  created() {
    // alert(this.postId)
    if (this.postId != undefined) {
      // 用户修改贴文,发送请求获取用户要修改的贴文回显
      this.initPostByPostId(this.postId);
    }
  },
};
</script>

<style>
.uploadPostContainer {
  margin-top: 30px;
}
.typeContainer {
  display: flex;
  align-items: center;
  margin: 10px;
  margin-bottom: 30px;
}
.contentInput {
  display: flex;
  align-items: center;
  margin: 10px;
  width: 700px;
}
.uploadPostContainer span {
  width: 70px;
  text-align: center;
}
.btn {
  height: 40px;
  width: 220px;
  background: blue;
  border-radius: 20px;
  text-align: center;
  line-height: 40px;
  color: white;
  margin: auto;
}
</style>